package client

import (
	"github.com/golang/glog"
	"github.com/golang/protobuf/proto"
	"github.com/oikomi/FishChatServer2/common/ecode"
	"github.com/oikomi/FishChatServer2/protocol/external"
	"github.com/oikomi/FishChatServer2/protocol/rpc"
	//"github.com/oikomi/FishChatServer2/server/access/conf"
	"github.com/oikomi/FishChatServer2/server/access/global"
	//
)

/*
func CheckMsg() {
	if err = proto.Unmarshal(reqData, reqLogin); err != nil {
		if err = c.Session.Send(&external.Error{
			Cmd:     external.ErrServerCMD,
			ErrCode: ecode.ServerErr.Uint32(),
			ErrStr:  ecode.ServerErr.String(),
		}); err != nil {
			glog.Error(err)
		}
		return
	}
}
*/
func (c *Client) procReqHallLogin(reqData []byte) (err error) {
	req := &external.ReqHallLogin{}

	if err = proto.Unmarshal(reqData, req); err != nil {
		if err = c.Session.Send(&external.Error{
			Cmd:     external.ErrServerCMD,
			ErrCode: ecode.ServerErr.Uint32(),
			ErrStr:  ecode.ServerErr.String(),
		}); err != nil {
			glog.Error(err)
		}
		return
	}

	glog.Info("halllogin data is %v", req)
	reqLoginRpc := &rpc.HallLoginReq{
		UID: req.UID,
		Pwd: req.Pwd,
	}
	resLoginRPC, err := c.RPCClient.Hall.Login(reqLoginRpc)
	if err != nil {
		if err = c.Session.Send(&external.Error{
			Cmd:     external.ErrServerCMD,
			ErrCode: ecode.ServerErr.Uint32(),
			ErrStr:  ecode.ServerErr.String(),
		}); err != nil {
			glog.Error(err)
		}
		glog.Error(err)
		return
	}

	if err = c.Session.Send(&external.Error{
		Cmd:     external.LoginCMD,
		ErrCode: resLoginRPC.ErrCode,
		ErrStr:  resLoginRPC.ErrStr,
	}); err != nil {
		glog.Error(err)

	}
	// success
	global.GSessions2[resLoginRPC.UID] = c.Session
	c.UID = resLoginRPC.UID
	return
}

func (c *Client) procReqCreateGameTable(reqData []byte) (err error) {
	req := &external.ReqCreateGameTable{}

	if err = proto.Unmarshal(reqData, req); err != nil {
		if err = c.Session.Send(&external.Error{
			Cmd:     external.ErrServerCMD,
			ErrCode: ecode.ServerErr.Uint32(),
			ErrStr:  ecode.ServerErr.String(),
		}); err != nil {
			glog.Error(err)
		}
		return
	}

	resCreateTableRPC, err := c.RPCClient.Hall.CreateTable(c.UID)
	if err != nil {
		if err = c.Session.Send(&external.Error{
			Cmd:     external.ErrServerCMD,
			ErrCode: ecode.ServerErr.Uint32(),
			ErrStr:  ecode.ServerErr.String(),
		}); err != nil {
			glog.Error(err)
		}
		glog.Error(err)
		return
	}

	if err = c.Session.Send(&external.Error{
		Cmd:     external.HallCreateTable,
		ErrCode: ecode.ServerErr.Uint32(),
		ErrStr:  ecode.ServerErr.String(),
	}); err != nil {
		glog.Error(err)
	}
	//创建房间返回进入逻辑服的ADDR
	c.LogicAddr = resCreateTableRPC.LogicAddr
	return
}

func (c *Client) procReqEnterTable(reqData []byte) (err error) {
	req := &external.ReqEnterTable{}
	if err = proto.Unmarshal(reqData, req); err != nil {
		if err = c.Session.Send(&external.Error{
			Cmd:     external.ErrServerCMD,
			ErrCode: ecode.ServerErr.Uint32(),
			ErrStr:  ecode.ServerErr.String(),
		}); err != nil {
			glog.Error(err)
		}
		return
	}

	resEnterTableRPc, err := c.RPCClient.Hall.EnterTable(c.UID, req.TableID)
	if err != nil {
		if err = c.Session.Send(&external.Error{
			Cmd:     external.ErrServerCMD,
			ErrCode: ecode.ServerErr.Uint32(),
			ErrStr:  ecode.ServerErr.String(),
		}); err != nil {
			glog.Error(err)
		}
		glog.Error(err)
		return
	}

	if err = c.Session.Send(&external.Error{
		Cmd:     external.HallEnterTable,
		ErrCode: resEnterTableRPc.ErrCode,
		ErrStr:  resEnterTableRPc.ErrStr,
	}); err != nil {
		glog.Error(err)
	}

	c.LogicAddr = resEnterTableRPc.LogicAddr
	return
}
